import gameConfig, {JumpMiniGameConfig} from "../../engines/configs/gameConfig";
import cellMiniGameJump from "../public/cellMiniGameJump";

const {ccclass, property} = cc._decorator;

@ccclass
export default class dialogMiniGameJump extends cc.Component {

    @property(cc.Prefab)
    pbThreeItem: cc.Prefab = null;
    @property(cc.Node)
    ndListContent: cc.Node = null;
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    protected start() {
        this.showAllDialogMiniGameJumpList();
    }

    showAllDialogMiniGameJumpList() {
        const LIST = gameConfig.MINI_GAME_JUMP_SIDE_LIST;
        cc.Tween.stopAllByTarget(this.ndListContent);
        for (let i = 0; i < LIST.length; i += 4) {
            let oneData = LIST[i];
            let twoData = LIST[i + 1];
            let threeData = LIST[i + 2];
            let fourData = LIST[i + 3];
            //
            let node = cc.instantiate(this.pbThreeItem);
            this.ndListContent.addChild(node);
            //
            this.controlCellOne(node.children[0], oneData);
            this.controlCellOne(node.children[1], twoData);
            this.controlCellOne(node.children[2], threeData);
            this.controlCellOne(node.children[3], fourData);
        }
        //下一帧轮播
        this.scheduleOnce(() => {
            if (this.ndListContent.height <= this.ndListContent.parent.height) return;
            let length = this.ndListContent.height - this.ndListContent.parent.height;
            //
            cc.tween(this.ndListContent)
                .then(
                    cc.tween(this.ndListContent)
                        .to(0, {y: 205})
                        .to(this.ndListContent.height / this.ndListContent.parent.height * 6, {y: 205 + length})
                )
                .repeatForever()
                .start();
        });
    }

    protected controlCellOne(node: cc.Node, data: JumpMiniGameConfig) {
        if (data == null) {
            node.active = false;
            return;
        }
        //
        node.getComponent(cellMiniGameJump).setData(data);
    }

    protected onBtnClose() {
        this.node.destroy();
    }

    // update (dt) {}
}
